from flask import Flask, request, jsonify
import subprocess
import os

app = Flask(__name__)

base_path = "/Users/caozy/Documents/projects/ai_db/vue3_data_chat"

# 定义API路径
@app.route('/ask', methods=['POST'])
def ask():
    # 获取POST请求中的数据
    data = request.json.get('data', '')
    
    # 定义文件路径
    long_question_path = os.path.join(base_path, "long_question.md")
    output_path = os.path.join(base_path, "output.md")
    
    # 将数据写入long_question.md文件
    with open(long_question_path, 'w') as file:
        file.write(data)
    
    # 执行命令行程序
    subprocess.run(["cli_assistant", "-u", "0"], cwd=base_path, check=True)
    
    # 读取output.md文件的内容
    with open(output_path, 'r') as file:
        output_content = file.read()
    
    # 返回output.md文件的内容
    return jsonify({ "message": output_content})

if __name__ == '__main__':
    # 启动服务，监听端口8000
    app.run(host='0.0.0.0', port=8000)
